﻿#include "widget.h"
#include "ui_widget.h"

#include <QFileDialog>
#include <qstringutils.h>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    progressMonitorThread = new ProgressMonitorThread();
    qRegisterMetaType<ProgressInfo>("ProgressInfo");
    connect(progressMonitorThread,&ProgressMonitorThread::signal_progressInfo,this,&Widget::slot_progressInfo);

    progressMonitorThread->start();
}

Widget::~Widget()
{
    delete progressMonitorThread;
    delete ui;
}

void Widget::slot_progressInfo(ProgressInfo progressInfo)
{
    ui->cpuUse->setValue(progressInfo.getCpuUsage());
    ui->cpuUse->setFormat(QString("(进程)CPU:%1%").arg(QString::number(progressInfo.getCpuUsage(),'f',2)));
    ui->memory->setText(QStringUtils::numberDivision(progressInfo.getMemory(),3,0)+" B");
    ui->ioRead->setText(QStringUtils::numberDivision(progressInfo.getIoRead(),3,0)+" B");
    ui->IoWrite->setText(QStringUtils::numberDivision(progressInfo.getIoWrite(),3,0)+" B");
    ui->state->setText(progressInfo.getEnabled() ? QString("运行") : QString("停止"));
    ui->stateMsg->setText(progressInfo.getEnabled() ? QStringUtils::formatBySecond(progressInfo.getRunTime()) : progressInfo.getStateMsg());
    ui->state->setStyleSheet(progressInfo.getEnabled() ? "color:green;" : "color:red");
    ui->stateMsg->setStyleSheet(progressInfo.getEnabled() ? "color:green;" : "color:red");
}


void Widget::on_progressSelBtn_clicked()
{
    QString path = ui->progressPath->text();
    QString openDir="";
    if(!path.isEmpty()){
        openDir = QFileInfo(path).path();
    }
    path = QFileDialog::getOpenFileName(this,QString("选择文件"),openDir,QString("应用程序(*.exe);;所有文件(*.*)"));
    if(!path.isEmpty()){
        ui->progressPath->setText(path);
        progressMonitorThread->setProgressPath(path);
    }
}
